package com.suiwei.webservice.config;

import com.suiwei.webservice.base.wsServer.interceptor.AuthInterceptor;
import com.suiwei.webservice.base.wsServer.GreetWebService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Resource
    private GreetWebService greetWebService;//注入业务实例

    @Resource
    private Bus bus;//注入bus接口实例
    @Resource
    private AuthInterceptor authInterceptor;//认证拦截器

    /**
     * webservice访问的父路径
     * tomcat启动的服务的路径http://127.0.0.1/services
     * @return
     */
    @Bean
    public ServletRegistrationBean getRegistrationBean(){
        return new ServletRegistrationBean(new CXFServlet(),"/services/*");
    }

    /**
     * 发布服务
     * 设置当前终端
     * @return
     */
    @Bean
    public Endpoint userServiceEndpoint() {
        System.out.println("服务发布");
        //这里指定的端口不能跟应用的端口冲突, 单独指定，jetty启动的端口路径
        String path = "http://127.0.0.1:9090/greet";

        EndpointImpl userEndpoint = new EndpointImpl(bus, greetWebService);
        userEndpoint.publish(path);
        userEndpoint.getInInterceptors().add(authInterceptor);//访问的拦截器

        System.out.println("服务成功，path: " + path);
        System.out.println(String.format("在线的wsdl：%s?wsdl", path));
        return userEndpoint;
    }
}